import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import jwtDecode from "jwt-decode";


const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [0]
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },



    CLEAR_TOKEN: (state) => {
      state.token = ''
    },
    CLEAR_NAME: (state) => {
      state.name = ''
    },
    CLEAR_AVATAR: (state) => {
      state.avatar = ''
    },
    CLEAR_ROLES: (state) => {
      state.roles = []
    },


  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {

/*      const data = {'token':'admin'}
      setToken(data.token)*/
      /*commit('SET_TOKEN', userInfo)*/

      const username = userInfo.username.trim() //除去空格
      return new Promise((resolve, reject) => {


          login(username, userInfo.password).then(response => {

          const data = response.data;
          let token=response.headers.token;
          // console.log("this is a data:");
          // console.log(data);
          setToken(token)


          commit('SET_TOKEN', token);

          resolve(response)//resolve可以使得这个new promise对象在外部使用then 来获取resolve传进去的参数
        }).catch(error => {
          // console.log(error.data)
          reject(error)
        })
      })

    },

    // 获取用户信息
    GetInfo({ commit },payload) {






      return new Promise((resolve, reject) => {

        let userInfo=jwtDecode(payload)
        const data = {'roles':'admin','name':'admin','avatar':'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'}
        /*      if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
                commit('SET_ROLES', data.roles)
              } else {
                reject('getInfo: roles must be a non-null array !')
              }*/
        // if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
        //   // commit('SET_ROLES', data.roles)
        // } else {
        //   reject('getInfo: roles must be a non-null array !')
        // }
        let temp=[];

        temp.push(userInfo.roles)
        console.log("roles2")
        console.log(temp)
        commit('SET_ROLES', temp)
        commit('SET_NAME', userInfo.username)
        commit('SET_AVATAR', data.avatar)//使用默认头像的吧

      })

    },

    // 登出
    LogOut({ commit, state }) {
      commit('CLEAR_TOKEN')
      commit('CLEAR_ROLES')
      commit('CLEAR_NAME')
      commit('CLEAR_AVATAR')
      removeToken()
      // return new Promise((resolve, reject) => {
      //   logout(state.token).then(() => {
      //     commit('SET_TOKEN', '')
      //     commit('SET_ROLES', [])
      //     removeToken()
      //     resolve()
      //   }).catch(error => {
      //     reject(error)
      //   })
      // })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      //return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        // resolve()
      //})
    }
  }
}

export default user
